package cn.tedu.egmvc3.controller;

import cn.tedu.egmvc3.base.config.response.JsonResult;
import cn.tedu.egmvc3.base.config.response.StatusCode;
import cn.tedu.egmvc3.mapper.UserMapper;
import cn.tedu.egmvc3.pojo.dto.UserAddParam;
import cn.tedu.egmvc3.pojo.dto.UserAddQuery;
import cn.tedu.egmvc3.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc3.pojo.entity.User;
import cn.tedu.egmvc3.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Api(tags = "用户管理模块")
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    UserMapper userMapper;
    @PostMapping("add")
    @ApiOperation(value = "新增资讯")
    @ApiOperationSupport(order = 10)
    public JsonResult add(UserAddParam userAddParam){
        System.out.println("userAddParam = " + userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        int num = userMapper.insert(user);
        if (num>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }



    }
    @GetMapping("list")
    @ApiOperation(value = "资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(UserAddQuery userListV1){
        String username = userListV1.getUsername();
        String password = userListV1.getPassword();
        List<UserListVO> userListVOS = userMapper.list(username, password);
        return JsonResult.ok(userListVOS);

    }

    @PostMapping("del")
    @ApiOperation(value ="删除资讯")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "Long")
    public JsonResult del(Long id){
        System.out.println("id = " + id);
        int i = userMapper.del(id);
       if (i>0){
           return JsonResult.ok(i);
       }else{
           return new JsonResult(StatusCode.OPERATION_FAIL);
       }


    }

    /**
     * 4.更新指定用户功能
     */
    @PostMapping("update")
    @ApiOperation(value = "更新用户")
    @ApiOperationSupport(order = 40)
    public JsonResult update(@ApiIgnore UserUpdateParam userUpdateParam){
        System.out.println("userUpdateParam = " + userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(LocalDateTime.now());
        int i = userMapper.update(user);

        if(i>0){
            return JsonResult.ok(i);

        }else {
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }

    }




}
